<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/2/10
 * Time: 9:57
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\file\controller;

use app\api\controller\Base;
use app\common\business\SeatDialog;
use app\common\lib\Excel;
use app\common\lib\Radom;
use app\common\lib\Show;
use app\common\lib\Status;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Exception;

class Index extends Base
{
    public function index()
    {
        halt("不能修改");
        $key_name = 5;
        $path = public_path('upload') . "{$key_name}.xlsx";
        $objReader = IOFactory::createReader('Xlsx');
        $objReader->setReadDataOnly(TRUE);
        $objPhpExcel = $objReader->load($path);
        $sheet = $objPhpExcel->getSheet(0);   //excel中的第一张sheet
        $highestRow = $sheet->getHighestRow();       // 取得总行数
        $highestColumn = $sheet->getHighestColumn();   // 取得总列数
        Coordinate::columnIndexFromString($highestColumn);
        $numEx = Excel::excel2Int($highestColumn);
        //dump(Excel::IntToChr(27));exit();
        $row_j = 0;
        $x = 0;
        $y = 0;
        //新
        for ($j = 0; $j <= $highestRow; $j++) {   //y轴
            $number = 1;
            $y++;   //代表y
            $row_j++;   //代表y坐标

            for ($i = 0; $i <= $numEx; $i++) {  //x轴
                $row = Excel::IntToChr($i);   //数字转字母
                $value = $objPhpExcel->getActiveSheet()->getCell($row . $y)->getValue();
                $data[$j][$i] = [
                    "YCoord" => $y,
                    "XCoord" => $i + 1,
                    "RowNum" => $row_j,
                    "SeatCode" => Radom::RandNumberStr(),
                    "type"=>$key_name
                ];
                if ($value) {
                    $data[$j][$i]['Status'] = 0;
                    $data[$j][$i]['ColumnNum'] = $number;
                    $number++;
                } else {
                    $data[$j][$i]['ColumnNum'] = $number;
                    $data[$j][$i]['Status'] = -1;
                }
            }
            $l = array_unique(array_column($data[$j], 'Status'));
            if (count($l) === 1 && $l[0] == -1) {
                unset($data[$j]);
                $row_j--;
            }
            $number = 1;
        }
//        dump($data);exit();
//
//        for ($j = 0; $j <= $highestRow; $j++) {   //y轴
//            $number = 1;
//            $row_j++;
//            for ($i = 0; $i <= $numEx; $i++) {  //x轴
//                $row = Excel::IntToChr($i);
//                $jj = $j + 1;
//                $value = $objPhpExcel->getActiveSheet()->getCell($row . $jj)->getValue();
//
//                $data[$j][$i] = [
//                    "YCoord" => $row_j,
//                    "XCoord" => $i + 1,
//                    "RowNum" => $row_j,
//                    "SeatCode" => Radom::RandNumberStr()
//                ];
//                if ($value) {
//                    $data[$j][$i]['Status'] = 0;
//                    $data[$j][$i]['ColumnNum'] = $number;
//                    $number++;
//                } else {
//                    $data[$j][$i]['ColumnNum'] = $number;
//                    $data[$j][$i]['Status'] = -1;
//                }
//            }
//
//            //判断一排中是否全部为-1
//            $l = array_unique(array_column($data[$j], 'Status'));
//            if (count($l) === 1 && $l[0] == -1) {
//                unset($data[$j]);
//            }
//            $number = 1;
//        }
//        dump($data);
//        exit();
        $lists = [];
        foreach ($data as $value) {
            foreach ($value as $v) {
                $lists[] = $v;
            }
        }
        try {
            $result = (new SeatDialog())->addSaveAll($lists);
        } catch (\Exception $e) {
            throw new Exception("座位类型");
        }
        if ($result) {
            return Show::json_show(Status::status_normal, "ok", ["result" => $result]);
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }
}
